//
//  noticeTypeViewController.m
//  ZhenPin
//
//  Created by tmosport on 14-7-11.
//  Copyright (c) 2014年 zhenpin. All rights reserved.
//

#import "noticeTypeViewController.h"

@interface noticeTypeViewController ()
@property (strong, nonatomic) IBOutlet UITableView *typeListTV;
@property(nonatomic,retain)NSArray *typeListArr;

@end

@implementation noticeTypeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"消息开关设置";
    [self setNavigationItem];
    // Do any additional setup after loading the view.
    [self dataInit];
}

-(void)setNavigationItem {
    [self.navigationController.navigationBar setBackIndicatorImage:
     [UIImage imageNamed:@"return"]];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:
     [UIImage imageNamed:@"return"]];
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
}

-(void)leftBarbuttonItemClicked:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)dataInit{
    NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
    NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
    NSString *memberId = [dataDic valueForKey:@"memberId"];
    NSString *atoken = [dataDic valueForKey:@"token"];
    
    NSDictionary *params = @{@"memberid": memberId,
                             @"access_token": atoken,
                             };
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger GET:APIURL@"myzhenpin/notice/getNoticeTypeList.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        _typeListArr = (NSArray *)responseObject;
        [self.typeListTV reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //DO:错误捕获
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_typeListArr count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *cellDic = [_typeListArr objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
    UILabel *outerName = (UILabel *)[cell viewWithTag:1001];
    UISwitch *status = (UISwitch *)[cell viewWithTag:1002];
    [status addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    status.tag = [cellDic[@"noticeTypeId"] intValue];
    outerName.text =[NSString stringWithFormat:@"%@",cellDic[@"outerName"]];
    NSString *tempS = [NSString stringWithFormat:@"%@",cellDic[@"flag"]];
    if ([tempS isEqualToString:@"1"]) {
        status.on = YES;
    }else{
        status.on = NO;
    }
    return cell;
}

- (void)switchAction:(UISwitch *)sender
{
    
    NSString *switchTag = [NSString stringWithFormat:@"%d",sender.tag];
    NSString *status = sender.on ? @"true" : @"false";
    NSString *types = [NSString stringWithFormat:@"%@:%@",switchTag,status];
    NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
    NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
    NSString* memberId = [dataDic valueForKey:@"memberId"];
    NSString* _atoken = [dataDic valueForKey:@"token"];
    
    NSDictionary *params = @{@"memberid": memberId,
                             @"access_token": _atoken,
                             @"types":types,
                             };
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    [manger POST:APIURL@"myzhenpin/notice/setNoticeTypeStatus.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //        _typeListArr = (NSArray *)responseObject;
        //        [self.typeListTV reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //DO(@"Error:%@", error);
    }];
    
    
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
